//
//  mimaCZ.m
//  企业信息查询
//
//  Created by 梁中豪 on 16/4/23.
//  Copyright © 2016年 梁中豪. All rights reserved.
//

#import "mimaCZ.h"
#import <BmobSDK/Bmob.h>

@interface mimaCZ ()
@property (weak, nonatomic) IBOutlet UITextField *yuanmima;
@property (weak, nonatomic) IBOutlet UITextField *newmima;
@property (weak, nonatomic) IBOutlet UITextField *queren;

- (IBAction)mima2FiledReturnEditing:(id)sender;//按下mima2检测两次输入密码是否一致
- (IBAction)TJMM:(id)sender;


@end

@implementation mimaCZ

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)textFiledReturnEditing:(id)sender {
    [sender resignFirstResponder];
}

- (IBAction)backgroundTap:(id)sender {
    [_yuanmima resignFirstResponder];
    [_newmima resignFirstResponder];
    [_queren resignFirstResponder];
}

- (IBAction)mima2FiledReturnEditing:(id)sender{
    if (_newmima.text==_queren.text) {
        [sender resignFirstResponder];
    } else {
        UIAlertController *mimaYZ = [UIAlertController
                                     alertControllerWithTitle:@"错误"
                                     message:@"两次密码输入不一致"
                                     preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"确定"
                                                            style:UIAlertActionStyleCancel
                                                          handler:^(UIAlertAction *action){
                                                          }];
        [mimaYZ addAction:yesAction];
        //显示
        [self presentViewController:mimaYZ animated:true completion:nil];
    }
    
}


- (IBAction)TJMM:(id)sender {
    if ((!(_yuanmima.text.length==0||_newmima.text.length==0||_queren.text.length==0))&&(_newmima.text==_queren.text)) {
        BmobUser *user2 = [BmobUser getCurrentUser];
        if(user2){
            NSString *temp1 =_yuanmima.text;
            NSString *temp2 =_newmima.text;
        [user2 updateCurrentUserPasswordWithOldPassword:temp1 newPassword:temp2 block:^(BOOL isSuccessful, NSError *error) {
            if (isSuccessful) {
                UIAlertController *mimaCZ = [UIAlertController
                                             alertControllerWithTitle:@"成功"
                                             message:@"密码修改成功"
                                             preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"确定"
                                                                    style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction *action){
                                                                      NSLog(@"success2");
                                                                      [self.navigationController popViewControllerAnimated:YES];
                                                                  
                                                                  }];
                [mimaCZ addAction:yesAction];
                //显示
                [self presentViewController:mimaCZ animated:true completion:nil];
                NSLog(@"success");
                //[self dismissViewControllerAnimated:YES completion:nil];
            } else {
                UIAlertController *mimaerror = [UIAlertController
                                             alertControllerWithTitle:@"错误"
                                             message:@"原密码错误，请重新输入"
                                             preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"确定"
                                                                    style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction *action){
                                                                  }];
                [mimaerror addAction:yesAction];
                //显示
                [self presentViewController:mimaerror animated:true completion:nil];
                NSLog(@"change password error:%@",error);
            }
        }];}
    }else{
        NSLog(@"youwenti");
    }

}
@end
